import Vue from 'vue'
import VueRouter from 'vue-router'

// import { getToken } from "@/utils/storage";

import Layout from "@/views/Layout"
import Login from "@/views/Login";
import Register from "@/views/Register";

import Index from "@/views/Index";
import Score from "@/views/Score"
import Student from "@/views/Student"
import Dashboard from "@/views/Dashboard"

import store from "@/store";



Vue.use(VueRouter)


const routes = [
  {
    path: "/index",
    component: Index,
    redirect: "/index/score",
    children: [
      { path: "/index/score", component: Score },
      { path: "/index/student", component: Student },
      { path: "/index/dashboard", component: Dashboard },
    ]
  },
  {
    path: "/",
    component: Layout,
    redirect: "/login",
    // 配置二级路由
    children: [
      { path: "login", component: Login },
      { path: "register", component: Register }
    ],
  },

]

const router = new VueRouter({
  routes
})

const whiteList = ["/login", "/register"];

// 设置导航守卫
router.beforeEach((to, from, next) => {
  // 判断是否有token 或 前往路径为白名单
  // console.log(store);
  const token = store.state.user.token
  // 从 vuex 的 store 中提取持久化 token
  if (token || whiteList.includes(to.path)) {
    next()
  } else {
    next("/login")
  }
})

export default router
